﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyCourseRegForm
 * 类型：       控制层
 * 功能说明：   监控
 * 作者：       郭晓庆
 * 创建日期：   2008/02/01
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Utils;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Common.Dao;
using Shpm.App.Common.Bean;

public partial class App_Input_HappyCourseRegForm : System.Web.UI.Page
{
    protected const string AUDITING_TYPE = "01";
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if(!IsPostBack)
        {
            HappyCourseDao dao = new HappyCourseDao();
            HappyCourseBean bean = new HappyCourseBean();
            bean.AuditingType = AUDITING_TYPE;
            IList list = dao.FindCurriculums(bean);
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();

            this.txtMemberNo.Attributes.Add("onblur", "showMemberName(this.id,'" + this.txtMemberName.ClientID + "');");

            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();

            paramBean.Catalog = "CURRICULUM_TYPE";
            IList dataList = commonDictionaryDao.FindByCatalog(paramBean);
            if (dataList.Count > 0)
            {
                dataList.Insert(0, new CommonDictionaryBean());
            }

            dropCurriculmType.DataSource = dataList;
            dropCurriculmType.DataBind();
            this.txtTeacherId.Attributes.Add("onblur", "showTeacherName(this.id,'" + this.txtTeacherName.ClientID + "');");
            this.MultiView1.SetActiveView(View1);
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        int curriculumId = Convert.ToInt32((sender as GridView).DataKeys[row].Values[0]);
        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        bean.CurriculumId = curriculumId;
        bean = dao.GetCurriculum(bean);

        this.labCurriculumId.Text = bean.CurriculumId.ToString();
        this.labCurriculumName.Text = bean.CurriculumName;
        this.labCount.Text = bean.CurriculumCount.ToString();
        this.labTeacher.Text = bean.TeacherId.ToString();
        this.labObject.Text = bean.CurriculumObject;
        this.labDate.Text = DateUtils.convertToString(bean.BeginDate.Value);
        this.labAddress.Text = bean.CurriculumAddress;
        this.labContact.Text = bean.RelationName;
        this.labPhone.Text = bean.RelationTel;
        this.labFee.Text = bean.FeeWage.ToString();
        this.txtContent.Text = bean.CurriculumContent;


        //学员信息
        HappyCurriculumStudentBean happyCurriculumStudentBean = new HappyCurriculumStudentBean();
        happyCurriculumStudentBean.CurriculumId = curriculumId;
        IList list = dao.FindCurriculumStudent(happyCurriculumStudentBean);
        this.GridView2.PageIndex = 0;
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
        InitPage();
        this.MultiView1.SetActiveView(View2);
    }

    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        int studentId = Convert.ToInt32((sender as GridView).DataKeys[row].Values[0]);


        HappyCourseDao dao = new HappyCourseDao();
        HappyCurriculumStudentBean bean = new HappyCurriculumStudentBean();
        bean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        bean.StudentId = studentId;
        bean = dao.GetCurriculumStudent(bean);
       
        if(bean != null)
        {
            this.labStudentId.Text = studentId.ToString();
            this.txtMemberNo.Text = bean.MemberNo;
            //显示姓名
            SetMemberName(bean.MemberNo);
            this.txtName.Text = bean.StudenName;
            this.txtAge.Text = bean.StudenAge.ToString();
            this.txtPhone.Text = bean.StudenTel;
            this.txtName.Text = bean.StudenName;
            this.radSex.SelectedValue = bean.StudenSex;
            this.txtStatus.Text = bean.StudenStatus;
            //会员
            if (!string.IsNullOrEmpty(bean.MemberNo))
            {
                this.radType.SelectedIndex = 0;
                this.txtName.Enabled = false;
                this.txtName.BackColor = System.Drawing.Color.Gainsboro;
                this.txtMemberNo.Enabled = true;
                this.txtMemberNo.BackColor = System.Drawing.Color.Empty;

            }
            //非会员
            else 
            {
                this.txtName.Enabled = true;
                this.txtName.BackColor = System.Drawing.Color.Empty;
                this.txtMemberNo.Enabled = false;
                this.txtMemberNo.BackColor = System.Drawing.Color.Gainsboro;
                this.radType.SelectedIndex = 1;


            }
            
        }
        
    }
    //显示姓名
    private void SetMemberName(string memberNo)
    {
        CommonSearchDao dao = new CommonSearchDao();
        ComMemberBean bean = new ComMemberBean();
        bean.MemberNo = memberNo;
        bean = dao.FindMember(bean);
        if(bean != null)
        {
            this.txtMemberName.Text = bean.NameC;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        HappyCourseDao dao = new HappyCourseDao();
        IList list = dao.FindCurriculums(new HappyCourseBean());
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HappyCourseBean bean = (HappyCourseBean)e.Row.DataItem;
            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
           
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;

        HappyCourseDao dao = new HappyCourseDao();
        HappyCurriculumStudentBean happyCurriculumStudentBean = new HappyCurriculumStudentBean();
        happyCurriculumStudentBean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        IList list = dao.FindCurriculumStudent(happyCurriculumStudentBean);
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();

    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HappyCurriculumStudentBean bean = (HappyCurriculumStudentBean)e.Row.DataItem;
            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
            e.Row.Cells[3].Text = string.IsNullOrEmpty(bean.MemberNo)?"否":"是";
            e.Row.Cells[4].Text = "否";
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        this.MultiView1.SetActiveView(View1);
    }
    private void InitPage() 
    {
        this.labStudentId.Text = "";
        this.txtMemberNo.Text = "";
        this.txtName.Text = "";
        this.txtMemberName.Text = "";
        this.txtAge.Text = "";
        this.txtPhone.Text = "";
        this.txtStatus.Text = "";
        this.txtName.Enabled = false;
        this.txtName.BackColor = System.Drawing.Color.Gainsboro;
        this.radSex.SelectedIndex = 0;
        this.radType.SelectedIndex = 0;
      
    }
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        ComValidateChecker validator = new ComValidateChecker();
        
       
        if ("01".Equals(this.radType.SelectedValue))
        {
            validator.Add(new CheckRequired(this.txtMemberNo.Text, new ComMessage("E0082", "会员编号")));
        }
        else 
        {
            validator.Add(new CheckRequired(this.txtName.Text, new ComMessage("E0082", "姓名")));
        }
        validator.Add(new CheckLong(this.txtAge.Text, new ComMessage("E0036", "年龄")));
        validator.Add(new CheckPhone(this.txtPhone.Text, new ComMessage("E0036", "联系电话")));

        if (validator.DoValidator() == false)
        {
            // 输出校验失败信息
            ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
            return;
        }
        //会员存在检查
        HappyCourseDao dao = new HappyCourseDao();
        HappyCurriculumStudentBean bean = new HappyCurriculumStudentBean();


       


        

        //取得画面数据
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;


        bean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        bean.MemberNo = this.txtMemberNo.Text;
        bean.StudenName = this.txtName.Text;
        bean.StudenSex = this.radSex.SelectedValue;
        bean.StudenStatus = this.txtStatus.Text;
        if(!string.IsNullOrEmpty(this.txtAge.Text))
            bean.StudenAge =Convert.ToInt32(this.txtAge.Text);
        bean.StudenTel = this.txtPhone.Text;
        bean.UserId = loginId;
       // bean.StudenStatus = this.radType.SelectedValue;


        //写入
        if (string.IsNullOrEmpty(this.labStudentId.Text))
        {
            if ("01".Equals(this.radType.SelectedValue))
            {
                if (checkMemberNo() == false)
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                             HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtMemberNo.Text);
                    return;
                }
                HappyCurriculumStudentBean paramBean = new HappyCurriculumStudentBean();
                paramBean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
                paramBean.MemberNo = this.txtMemberNo.Text;
                paramBean = dao.GetCurriculumStudentByMemberNo(paramBean);
                if (paramBean != null)
                {
                    ((Design_MasterPage)this.Master).InfoMessage = "报名对象已存在，请确认！";

                    return;
                }

            }


            int studentId = (int)dao.InsCurriculumStudent(bean);

            this.labStudentId.Text = studentId.ToString();

        }
            //更新
        else 
        {
            if ("01".Equals(this.radType.SelectedValue))
            {
                if (checkMemberNo() == false)
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                             HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtMemberNo.Text);
                    return;
                }
                HappyCurriculumStudentBean paramBean = new HappyCurriculumStudentBean();
                paramBean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
                paramBean.MemberNo = this.txtMemberNo.Text;
                paramBean = dao.GetCurriculumStudentByMemberNo(paramBean);
                if (paramBean != null && paramBean.StudentId != Convert.ToInt32(this.labStudentId.Text))
                {
                    ((Design_MasterPage)this.Master).InfoMessage = "报名对象已存在，请确认！";

                    return;
                }

            }
            bean.StudentId = Convert.ToInt32(this.labStudentId.Text);
            dao.UpdCurriculumStudent(bean);
        }
       
       
        ((Design_MasterPage)this.Master).InfoMessage =
                               HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();

        //更新gridview2
        BindStudents();

        
        
    }

    private void BindStudents() 
    {
        HappyCourseDao dao = new HappyCourseDao();
        HappyCurriculumStudentBean happyCurriculumStudentBean = new HappyCurriculumStudentBean();
        happyCurriculumStudentBean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        IList list = dao.FindCurriculumStudent(happyCurriculumStudentBean);
        this.GridView2.PageIndex = 0;
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    private bool checkMemberNo()
    {
        MemberSrhDao dao = new MemberSrhDao();
        MemberSrhBean bean = new MemberSrhBean();
        bean.MemberNo = this.txtMemberNo.Text;
        if (dao.GetMember(bean) == null)
            return false;
        return true;
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        //会员
        if (this.radType.SelectedIndex == 0)
        {

            this.txtName.Enabled = false;
            this.txtName.BackColor = System.Drawing.Color.Gainsboro;
            this.txtMemberNo.Enabled = true;
            this.txtMemberNo.BackColor = System.Drawing.Color.Empty;
            this.searchImg.Visible = true;
        }
        //非会员
        else
        {
            this.txtName.Enabled =  true ;
            this.txtName.BackColor = System.Drawing.Color.Empty;
            this.txtMemberNo.Enabled = false;
            this.txtMemberNo.BackColor = System.Drawing.Color.Gainsboro;
            this.searchImg.Visible = false;


        }

    }
    protected void btn_New_Click(object sender, EventArgs e)
    {
        InitPage();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }

        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        bean.AuditingType = AUDITING_TYPE;
        bean.CurriculumName = string.IsNullOrEmpty(this.txtCurriculmName.Text.Trim()) ? null : this.txtCurriculmName.Text.Trim();
        bean.CurriculumType = string.IsNullOrEmpty(this.dropCurriculmType.SelectedValue) ? null : this.dropCurriculmType.SelectedValue;
        if (!string.IsNullOrEmpty(this.txtBeginDate.Text))
            bean.BeginDate = DateUtils.convertToDate(this.txtBeginDate.Text);
        if (!string.IsNullOrEmpty(this.txtEndDate.Text))
            bean.EndDate = DateUtils.convertToDate(this.txtEndDate.Text);
        IList list = dao.FindCurriculums(bean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void GridView2_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int studentId = Convert.ToInt32((sender as GridView).DataKeys[e.RowIndex].Values[0]);
        HappyCourseDao dao = new HappyCourseDao();
        HappyCurriculumStudentBean bean = new HappyCurriculumStudentBean();
        bean.StudentId = studentId;
        dao.DelCurriculumStudent(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
                               HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
        BindStudents();
    }
}
